package com.alex.algorithm.easy;

public class RemoveLinkedListElem {

    public static void main(String[] args) {
        LinkedListNode n1 = new LinkedListNode();
        n1.setVal(1);
        LinkedListNode n2 = new LinkedListNode();
        n1.setNext(n2);
        n2.setVal(2);
        LinkedListNode n22 = new LinkedListNode();
        n22.setVal(2);
        n2.setNext(n22);
        LinkedListNode n3 = new LinkedListNode();
        n3.setVal(3);
        n22.setNext(n3);
        LinkedListNode head = remove(n1, 2);


        while (head.getNext() != null) {
            System.out.println(head);
            head = head.getNext();
        }
    }

    public static LinkedListNode remove(LinkedListNode node, int val) {
        LinkedListNode dummy = new LinkedListNode();
        dummy.setVal(0);
        dummy.setNext(node);
        LinkedListNode cur = dummy;
        while(cur.getNext() != null) {
            if (cur.getNext().getVal() == val) {
                cur.setNext(cur.getNext().getNext());
            }else {
                cur = cur.getNext();
            }
        }

        return dummy.getNext();
    }
}
